package com.yidao.core.controller;

import javax.validation.Validator;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import com.yidao.user.domain.User;
import com.yidao.user.service.UserService;

public class BaseController {
	
	@Autowired
	private UserService userService;
	
	@Autowired
    protected Validator validator;
	
	public User getCurrentUser(){
		
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

		String userName = "";
		if (principal instanceof UserDetails) {
			userName = ((UserDetails)principal).getUsername();
		} else {
			userName = principal.toString();
		}
		
		User user = (User)userService.getByUserName(userName);
		return user;
	}
	
	protected Logger getLogger(){
		return Logger.getLogger(this.getClass());
	}
	
}
